function formatterStudent(value,row,index) {
    return value.studentName;
}
function formatterClz(value,row,index) {
    return value.name;
}
function formatterPaymentMethod(value,row,index) {
    return value.name;
}
function formatterCashier(value,row,index) {
    return value.realName;
}
function formatterStatus(value,row,index) {
    return value == 0 ? "<span style='color: red'>未审核</span>" : "<span style='color: green'>已审核</span>";
}
function formatterAuditer(value,row,index) {
    if (value) {
        return value.realName;
    }
    return "";
}


$(function () {
    //数据表格对象
    var tuitionIncome_datagrid = $("#tuitionIncome_datagrid");
    //工具栏对象
    var tuitionIncome_datagrid_toolbar = $("#tuitionIncome_datagrid_toolbar");
    //审核按钮
    var btn_changeState = $("#btn_changeState");
    //编辑按钮
    var btn_edit = $("#btn_edit");
    //删除按钮
    var btn_delete = $("#btn_delete");
    //编辑弹窗
    var tuitionIncome_dialog = $("#tuitionIncome_dialog");
    //编辑表单
    var tuitionIncome_add_form = $("#tuitionIncome_add_form");



    tuitionIncome_datagrid.datagrid({
        url:'/tuitionIncome/list',
        fitColumns:true,
        fit:true,
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        columns:[[
            {field:'receipt',title:'单据号',width:100},
            {field:'student',title:'学员姓名',width:100,formatter:formatterStudent},
            {field:'clz',title:'班级名称',width:100,formatter:formatterClz},
            {field:'amountMoney',title:'收款金额',width:100},
            {field:'remainPaid',title:'未缴学费',width:100},
            {field:'paymentMethod',title:'支付方式',width:100,formatter:formatterPaymentMethod},
            {field:'cashier',title:'经办人',width:100,formatter:formatterCashier},
            {field:'myRemark',title:'备注',width:100},
            {field:'status',title:'审核状态',width:100,formatter:formatterStatus},
            {field:'auditer',title:'审核人',width:100,formatter:formatterAuditer}
        ]],
        toolbar:tuitionIncome_datagrid_toolbar,
        onSelect:function (index, data) {
            //处于待审核状态
            if (data.status == 0) {
                btn_changeState.linkbutton({
                    text: "审核",
                    iconCls:"icon-audit"
                })
                $("#btn_edit").linkbutton({
                        text : "编辑",
                        iconCls:"icon-edit"
                    })
                $("#btn_delete").linkbutton({
                    text : "删除",
                    iconCls:"icon-remove"
                })
                //增加data-cmd属性,增加enable属性
                $("a[data-cmd='editIncome']").data("cmd","editIncome");
                $("a[data-cmd='editIncome']").linkbutton("enable");
                $("a[data-cmd='delete']").data("cmd","deleteIncome");
                $("a[data-cmd='delete']").linkbutton("enable");
            }else {
                //处于已审核状态
                btn_changeState.linkbutton({
                    text: "",
                    iconCls:null
                })
                btn_delete.linkbutton({
                    text : "",
                    iconCls:null,
                    disabled:true
                })
                btn_edit.linkbutton({
                    text: "",
                    iconCls: null,
                    disabled: true
                });
                //删除cmd属性,设置编辑按钮为不可编辑
                $("a[data-cmd='editIncome']").data("cmd", "");
                $("a[data-cmd='editIncome']").linkbutton("disable");
                $("a[data-cmd='deleteIncome']").data("cmd", "");
                $("a[data-cmd='deleteIncome']").linkbutton("disable");
            }
        }
    });
    var cmdObj = {
        //刷新列表
        reloadIncome:function () {
            tuitionIncome_datagrid.datagrid("reload");
        },
        //添加按钮
        addIncome:function () {
            tuitionIncome_add_form.form("clear");
            tuitionIncome_dialog.dialog({
                title:'新增学员缴费信息'
            })
            tuitionIncome_dialog.dialog("open");
        },
        cancelIncomeForm:function () {
            tuitionIncome_dialog.dialog("close");
        },
        //保存按钮
        saveIncomeForm:function () {
            var url = '/tuitionIncome/save';
            var id = $("#tuitionIncomeId").val();
            if (id) {
                url = '/tuitionIncome/update';
            }
            tuitionIncome_add_form.form('submit', {
                url:url,
                success: function (data) {
                    console.log($("#cc3"));
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            cmdObj.cancelIncomeForm();
                            cmdObj.reloadIncome();
                        });
                    } else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                }
            })
        },
        //编辑按钮
        editIncome:function () {
            //获取选中行
            var selectedRow = tuitionIncome_datagrid.datagrid("getSelected");
            console.log(selectedRow);
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            tuitionIncome_add_form.form("clear");
            //回显姓名
            selectedRow['student.id'] = selectedRow.student.id;
            //回显经办人
            selectedRow['cashier.id'] = selectedRow.cashier.id;
            //回显缴费方式
            selectedRow['paymentMethod.id'] = selectedRow.paymentMethod.id;
            //设置回显
            tuitionIncome_add_form.form("load",selectedRow);
            tuitionIncome_dialog.dialog({
                title:'编辑学员缴费信息'
            })
            tuitionIncome_dialog.dialog("open");
        },
        //删除按钮
        deleteIncome:function () {
            //获取选中行
            var selectedRow = tuitionIncome_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            $.messager.confirm("提示","确定要删除吗?",function (yes) {
                if (yes) {
                    $.post('/tuitionIncome/deleteById',{id:selectedRow.id},function (data) {
                        if (data.success) {
                            $.messager.alert("提示", "操作成功", "info", function () {
                                cmdObj.reloadIncome();
                            });
                        } else {
                            $.messager.alert("提示", data.msg, "error");
                        }
                    })
                }
            })
        },
        //审核按钮
        audit:function () {
            //获取选中行
            var selectedRow = tuitionIncome_datagrid.datagrid("getSelected");
            //判断是否选中行
            if (!selectedRow) {
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            $.messager.confirm("提示","确定审核通过这笔信息吗?",function (yes) {
                $.post("/tuitionIncome/auditById",{id:selectedRow.id},function (data) {
                    if (data.success) {
                        $.messager.alert("提示", "操作成功", "info", function () {
                            cmdObj.reloadIncome();
                        });
                    } else {
                        $.messager.alert("提示", data.msg, "error");
                    }
                })
            })
        }
    };
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    });
    //当学员姓名发生变化时,去查出学员的其他信息并设置到表单中
    var remainPaid;
    $("#cc1").combobox({
        onSelect: function(data){
            $("#cc4").val(data.remainPaid);
            $("#cc2").val(data.clz.name);
            $("#cc3").prop("value",data.clz.id);
            remainPaid = data.remainPaid;
        }
    })
    $("input[name='amountMoney']").blur(function () {
        $("#cc4").val((remainPaid - $("input[name='amountMoney']").val() ) >= 0 ? (remainPaid - $("input[name='amountMoney']").val() ) : "请输入正确的付款金额")
    })
})


function search(value) {
    $("#tuitionIncome_datagrid").datagrid("load", {
        keyword: value,
    });
}